package vista;

import org.uqbar.arena.actions.MessageSend;
import org.uqbar.arena.bindings.NotNullObservable;
import org.uqbar.arena.layout.ColumnLayout;
import org.uqbar.arena.layout.VerticalLayout;
import org.uqbar.arena.widgets.Button;
import org.uqbar.arena.widgets.Label;
import org.uqbar.arena.widgets.Panel;
import org.uqbar.arena.widgets.TextBox;
import org.uqbar.arena.windows.Dialog;
import org.uqbar.arena.windows.WindowOwner;

import applicationModels.EditarDisponibilidad;

public class EditarDisponibilidadWindow extends Dialog<EditarDisponibilidad> {

	private static final long serialVersionUID = 1L;

	public EditarDisponibilidadWindow(WindowOwner owner, EditarDisponibilidad model) {
		super(owner, model);
		this.setTitle("Editar disponibilidad");
		this.setTaskDescription("");
	}
	
	@Override
	protected void addActions(Panel actionsPanel) {
		Button acceptButton=new Button(actionsPanel);
		acceptButton.setCaption("Aceptar")
		.onClick(new MessageSend(this, "accept"))
		.setAsDefault()
		.disableOnError();
		
		NotNullObservable nuevoInicio=new NotNullObservable("nuevoInicio");
		NotNullObservable nuevoFin=new NotNullObservable("nuevoFin");
		acceptButton.bindEnabled(nuevoInicio);
		acceptButton.bindEnabled(nuevoFin);

		new Button(actionsPanel)
			.setCaption("Cancelar")
			.onClick(new MessageSend(this, "cancel"));		
	}

	@Override
	protected void createFormPanel(Panel mainPanel) {
		mainPanel.setLayout(new VerticalLayout());
		new Label(mainPanel).setText("Dia: "+this.getModelObject().getDia().toString());
		
		Panel horarioPanel=new Panel(mainPanel);
		this.createHorarioForm(horarioPanel);
	}

	private void createHorarioForm(Panel panel) {
		panel.setLayout(new VerticalLayout());
		new Label(panel).setText("Horario actual: "+new HorarioToStringTransformer().transform(this.getModelObject().getHorario()));
		
		Panel nuevoHorarioPanel=new Panel(panel);
		nuevoHorarioPanel.setLayout(new ColumnLayout(2));
		
		new Label(nuevoHorarioPanel).setText("Nuevo inicio");
		new TextBox(nuevoHorarioPanel)
			.setWidth(40)
			.bindValueToProperty("nuevoInicio")
			.setTransformer(new HoraToStringAdapter());

		
		new Label(nuevoHorarioPanel).setText("Nuevo fin");
		new TextBox(nuevoHorarioPanel)
			.setWidth(40)
			.bindValueToProperty("nuevoFin")
			.setTransformer(new HoraToStringAdapter());
	}
	
	@Override
	public void accept(){
		this.getModelObject().modificarDisponibilidad();
		super.accept();
	}
	
	

	
}
